home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / emstools.arc / SAMPLE.C < prev    next >
Text File  |  1990-02-04  |  9KB  |  200 lines

  1. /**************************************************************************/
  2. /* The intent of this example program is to show how to store and         */
  3. /* manipulate data in expanded memory using the C Memory Manager --       */
  4. /* MEMLIB.                                                                */
  5. /*                                                                        */
  6. /* This example shows what developers need to do in order to manipulate   */
  7. /* expanded memory in their own applications by using MEMLIB routines.    */
  8. /* For a more detailed program that uses a doubly linked list data        */
  9. /* structure, see the ROLODEX.C program.                                  */
  10. /*                                                                        */
  11. /* This program follows a seven-step algorithm:                           */
  12. /*   1.  Check the total amount of expanded memory available.             */
  13. /*   2.  Find the largest contiguous block of free expanded memory.       */
  14. /*   3.  Allocate expanded memory for two strings.                        */
  15. /*   4.  Calculate the size of the block allocated for the first string.  */
  16. /*   5.  Print the two strings.                                           */
  17. /*   6.  Manipulate the data by appending one string to the other.        */
  18. /*   7.  Print the new string.                                            */
  19. /**************************************************************************/
  20.  
  21. #include <stdio.h>
  22. #include <string.h>
  23.  
  24. /**************************************************************************/
  25. /* The two header files below contain the function prototypes for         */
  26. /* MEMLIB routines and the error messages specific to MEMLIB.  Keep in    */
  27. /* mind that MEMLIB calls EMMLIB functions, so you may get an error code  */
  28. /* relating to EMS.  (EMS codes are explained in Table A-2 in the         */
  29. /* EMS manual.)  You must include these two headers for any application   */
  30. /* that will use MEMLIB.  If you are going to use any EMMLIB calls        */
  31. /* directly, you must also include "emmlib.h."                            */
  32. /**************************************************************************/
  33.  
  34. #include "memlib.h"   
  35. #include "errors.h"
  36.  
  37. #define PASSED          0           /* If the MEMLIB call was successful */
  38.                                     /* it will return a zero.            */
  39. #define MAX_STRING_SIZE 255
  40.  
  41. void sample_abort (unsigned int);  /* If we encounter any errors, we    */
  42.                                    /* call this function to "clean up." */
  43.  
  44. void main()
  45. {
  46.    unsigned int  status;           /* The status after a MEMLIB call.   */
  47.    char          *string1;         /* Our first data item.              */
  48.    char          *string2;         /* Our second data item.             */
  49.    unsigned int  token1;           /* The identifier to our first       */
  50.                                    /* block of expanded memory.         */
  51.    unsigned int  token2;           /* The identifier to our second      */
  52.                                    /* block of expanded memory.         */
  53.    unsigned int  max_block_size;   /* The size of the largest           */
  54.                                    /* allocatable contiguous block.     */
  55.    unsigned int  size;             /* The size of one of our blocks.    */
  56.    unsigned long max_exp_mem;      /* The amount of expanded mem avail. */
  57.   
  58.  
  59.    /***********************************************/
  60.    /* See how much total expanded memory we have. */
  61.    /***********************************************/
  62.  
  63.    status = ememavl (&max_exp_mem);
  64.    if (status != PASSED)
  65.    {
  66.       printf ("Unable to obtain total expanded memory size.\n");
  67.       sample_abort (status);
  68.    }
  69.    else 
  70.       printf ("Total expanded memory available: %lu\n", max_exp_mem);
  71.  
  72.    /*************************************************/
  73.    /* Get the size of the largest contiguous block. */
  74.    /*************************************************/
  75.  
  76.    status = ememmax (&max_block_size);
  77.    if (status != PASSED)
  78.    {
  79.       printf ("Unable to obtain the largest contiguous block.\n");
  80.       sample_abort (status);
  81.    }
  82.    else 
  83.       printf ("Largest contiguous block: %u\n\n", max_block_size);
  84.  
  85.    /*********************************************/
  86.    /* Allocate the memory and check for errors. */
  87.    /*********************************************/
  88.  
  89.    status = efmalloc (MAX_STRING_SIZE, &token1);
  90.    if (status != PASSED)
  91.    {
  92.       printf ("Unable to allocate first block.\n");
  93.       sample_abort (status);
  94.    }
  95.    else
  96.       printf ("Allocated space for the first block.\n");
  97.  
  98.    status = efmalloc (MAX_STRING_SIZE, &token2);
  99.    if (status != PASSED)
  100.    {
  101.       printf ("Unable to allocate second block.\n");
  102.       sample_abort (status);
  103.    }
  104.    else
  105.       printf ("Allocated space for the second block.\n\n");
  106.  
  107.    /********************************/
  108.    /* Check the size of block one. */
  109.    /********************************/
  110.  
  111.    status = emsize (token1, &size);
  112.    if (status != PASSED)
  113.    {
  114.       printf ("Unable to obtain size for block %u\n", token1);
  115.       sample_abort (status);
  116.    }
  117.    else
  118.       printf ("Size of block %u is %u\n\n", token1, size);
  119.  
  120.    /***********************************************/
  121.    /* Map in the two blocks, checking for errors. */
  122.    /***********************************************/
  123.  
  124.    status = set2eptrs (token1, token2, (void**) &string1,(void**) &string2);
  125.    if (status != PASSED)
  126.    {
  127.       printf ("Unable to map in the two blocks.\n");
  128.       sample_abort (status);
  129.    }
  130.    else
  131.       printf ("Blocks mapped in.\n\n");
  132.  
  133.    printf ("Putting data in expanded memory.\n\n");
  134.  
  135.    /*********************************************/
  136.    /* Store values in expanded memory using the */ 
  137.    /* pointers given to us from set2eptrs().    */
  138.    /*********************************************/
  139.  
  140.    strcpy (string1, "What if life is an illusion and nothing exists?\n");
  141.    strcpy (string2, 
  142.            " 'In that case, I definitely overpaid for my carpet.' --Woody Allen");
  143.  
  144.    printf ("String1: %s\n", string1);
  145.    printf ("String2: %s\n\n", string2);
  146.    
  147.    /**************************************************/
  148.    /* Still using the pointers, manipulate the data. */
  149.    /**************************************************/
  150.  
  151.    printf ("Manipulating data in expanded memory.\n");
  152.  
  153.    strcat (string1, string2);
  154.  
  155.    printf ("String1: %s\n\n", string1);
  156.  
  157.    /*************************************************************/
  158.    /* Free all expanded memory we've allocated.  If effreeall() */
  159.    /* is unsuccessful, we don't want to call sample_abort()     */
  160.    /* because sample_abort() would just call effreeall() again. */
  161.    /*************************************************************/
  162.  
  163.    status = effreeall();
  164.    if (status != PASSED)
  165.    {
  166.       printf ("Unable to free memory.\n");
  167.       printf ("ERROR %X\n", status);
  168.       exit (2);
  169.    }
  170.    else
  171.       printf ("Expanded memory freed.\n");
  172. }
  173.  
  174. /****************************************************************************/
  175. /* Aborts the program with an error message.  Effreeall() frees all         */
  176. /* pages, blocks, and handles associated with this application.  It is      */
  177. /* imperative that you release all the expanded memory you've allocated     */
  178. /* before exiting, so that other applications can use this memory.  If      */
  179. /* you don't, you'll have to reboot to recover the lost memory.  Notice     */
  180. /* also that a call to effreeall() checks the EMM status.  In case that     */
  181. /* call fails,  you'll get an error condtion back to let you know the       */
  182. /* memory was not freed.                                                    */
  183. /*                                                                          */
  184. /* You should always call effreeall() before aborting your program, even    */
  185. /* if you get an error before allocating any memory.  Effreeall() will free */
  186. /* the handles and memory that MEMLIB allocated when it was initialized.    */
  187. /****************************************************************************/
  188. void sample_abort (status)
  189. unsigned int status;
  190. {
  191.    printf ("ERROR %X\n", status);
  192.    status = effreeall();
  193.    if (status != PASSED)
  194.    {
  195.       printf ("ERROR %X from effreeall().\n");
  196.       printf ("Expanded memory not freed.\n");
  197.    }
  198.    exit (1);
  199. }
  200.